package User;

import MySQL_im.getInfo;
import User.FunctionPkg.AppButton;
import User.FunctionPkg.ShowAppPanel;
import User.FunctionPkg.VideoPlayer;
import Warning.ResultUI;
import Win.InitWin;
import Win.LogWin;
import javafx.stage.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class UserWin extends JFrame implements InitWin,MouseListener, ActionListener{
    String[] type = {"平和质","气虚质","阳虚质","阴虚质","痰湿质","湿热质","血瘀质","气郁质","特禀质"};
    String[] bntname={"images/数据调查.png","images/数据解析.png","images/词典搜索.png","images/个人数据库.png"};
    String[] btnt={"数据调查","数据解析","百科搜索","个人数据库"};
    AppButton[] appbtn = new AppButton[btnt.length];
    JButton[] app = new JButton[appbtn.length];
    private String name;
    JButton out = new JButton("退出登录");
    JButton why;
    String al_sum;
    ImageIcon touxiang;
    Font font = new Font("楷体",Font.PLAIN,18);
    JPanel user_info=new JPanel();
    JLabel YinYang;
    JPanel Function=new JPanel();
    getInfo info = new getInfo();
    Statement st = info.getStatement();
    Connection con = info.getConnection();
    String url=null;
    private char sex;
    private long age;
    double[] score = new double[9];
    public UserWin(String name){
        this.name = name;
        url = "SELECT * FROM user where name = '"+this.name+"'";
        ResultSet res = null;
        try {
            res = st.executeQuery(url);
            while(res.next()){
                int num = 7;
                sex = res.getString(5).charAt(0);
                al_sum = res.getString(2);
                for (int i = 0; i < score.length; i++) {
                    score[i] = res.getInt(num+i);
                }
                LocalDate birth = res.getDate(6).toLocalDate();
                LocalDate now = LocalDate.now();
                age = birth.until(now, ChronoUnit.YEARS);
            }
            init();
        } catch (SQLException e) {
            System.out.println(e.toString());
        }
    }
    @Override
    public void init() {
        UserInit();
        FunctionInit();
        setUndecorated(true);
        setBounds(100,100,width,height);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("中医体质管理系统");
        JSplitPane UserPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,false,user_info,Function);
        UserPane.setDividerLocation(300);
        UserPane.setOneTouchExpandable(false);
        UserPane.setDividerSize(0);
        UserPane.setEnabled(false);
        getContentPane().add(UserPane);
        user_info.add(setBkg("images/用户背景1.png"));
        Function.add(setBkg("images/用户背景2.png"));
        validate();
    }
//    用户信息界面
    public void UserInit(){
        user_info.setLayout(null);
//        用户头像
        if(sex=='男')
            touxiang = new ImageIcon("images/男性图像.png");
        else
            touxiang = new ImageIcon("images/女性图像.png");
        JLabel user_img = new JLabel(touxiang);
        user_img.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        user_img.setSize(touxiang.getIconWidth(), touxiang.getIconHeight());
        user_img.setBounds(70,20, touxiang.getIconWidth(), touxiang.getIconHeight());
        user_info.add(user_img);
//        用户基本信息
//        姓名
        JLabel name_info = new JLabel("姓名："+name,SwingConstants.LEFT);
        name_info.setFont(font);
        name_info.setBounds(30,200,240,20);
        user_info.add(name_info);
//        性别
        JLabel sex_info = new JLabel("性别："+sex,SwingConstants.LEFT);
        sex_info.setFont(font);
        sex_info.setBounds(30,220,240,20);
        user_info.add(sex_info);
//        年龄
        JLabel age_info = new JLabel("年龄："+age+"岁",SwingConstants.LEFT);
        age_info.setFont(font);
        age_info.setBounds(30,240,240,20);
        user_info.add(age_info);
//        体质类型得分
        JLabel score_t = new JLabel("最近一次测试结果：",SwingConstants.CENTER);
        score_t.setFont(new Font("宋体",Font.BOLD,18));
        score_t.setBounds(30,270,240,20);
        user_info.add(score_t);
//        实际分值
        JLabel[] score_arr = new JLabel[9];
        int le = 270;
        for (int i = 0; i < score.length; i++) {
            score_arr[i] = new JLabel(type[i]+"："+score[i],SwingConstants.LEFT);
            score_arr[i].setFont(font);
            le += 30;
            score_arr[i].setBounds(30,le,240,20);
            user_info.add(score_arr[i]);
        }
//        总评结果
        JLabel summary;
        if(al_sum==null)
            summary = new JLabel("暂时无法做出准确判断",SwingConstants.CENTER);
        else
            summary = new JLabel("最终结果：",SwingConstants.LEFT);
        summary.setFont(new Font("楷体",Font.BOLD,18));
        le += 30;
        summary.setBounds(30,le,240,20);
        user_info.add(summary);
        if(al_sum!=null){
            why = new JButton("点击查看");
            why.setBounds(150,le,130,20);
            why.setFont(new Font("楷体",Font.PLAIN,18));
            user_info.add(why);
            why.addActionListener(this);
        }
        out.setBounds(50,le+50,200,35);
        out.setFont(new Font("黑体",Font.BOLD,25));
        user_info.add(out);
        out.addMouseListener(this);
    }
    public void FunctionInit(){
        Function.setLayout(null);
//        按钮区
        JPanel but_area = new JPanel();
        setBtn_show(but_area);
        Function.add(but_area);
//        视频播放区
        ImageIcon YinYang_img = new ImageIcon("images/阴阳.png");
        YinYang = new JLabel("点击打开视频",YinYang_img,SwingConstants.CENTER);
        YinYang.setHorizontalTextPosition(SwingConstants.CENTER);
        YinYang.setVerticalTextPosition(SwingConstants.BOTTOM);
        YinYang.setFont(new Font("华文行楷",Font.BOLD,30));
        YinYang.setBounds(300,200,400,400);
        YinYang.addMouseListener(this);
        Function.add(YinYang);
    }
    public void mouseClicked(MouseEvent e){
        Object obj = e.getSource();
        if(obj==out){
            dispose();
            try {
                st.close();
                con.close();
            } catch (SQLException ex) {
                System.out.println(ex.toString());
            }
            new LogWin();
        }
        if(obj==YinYang){
            new VideoPlayer();
        }
    }
    public void setBtn_show(JPanel but_area){
        but_area.setLayout(null);
        JLabel title = new JLabel("功能区",SwingConstants.CENTER);
        title.setFont(new Font("楷体",Font.BOLD,28));
        title.setForeground(Color.WHITE);
        title.setBounds(0,20,150,30);
        but_area.add(title);
        int hi = 80;
        for (int i = 0; i < bntname.length; i++) {
            appbtn[i] = new AppButton(btnt[i],bntname   [i]);
            app[i] = appbtn[i].AppEnter();
            but_area.add(app[i]);
            app[i].setBounds(25,hi,100,100);
            app[i].addActionListener(
                e -> {
                    JButton ans = (JButton) e.getSource();
                    new ShowAppPanel(ans.getText(),name,this);
                }
            );
            hi += 120;
        }
        but_area.setBackground(new Color(0,0,0,70));
        but_area.setBounds(0,0,150,800);
    }
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public JLabel setBkg(String path){
        ImageIcon bk = new ImageIcon(path);
        JLabel bkg = new JLabel(bk);
        bkg.setSize(bk.getIconWidth(),bk.getIconHeight());
        bkg.setBounds(0,0,bkg.getWidth(),bkg.getHeight());
        return bkg;
    }
    public void UpdateInfo(){
        user_info.removeAll();
        ResultSet res = null;
        try {
            res = st.executeQuery(url);
            while(res.next()){
                int num = 7;
                sex = res.getString(5).charAt(0);
                al_sum = res.getString(2);
                for (int i = 0; i < score.length; i++) {
                    score[i] = res.getInt(num+i);
                }
                LocalDate birth = res.getDate(6).toLocalDate();
                LocalDate now = LocalDate.now();
                age = birth.until(now, ChronoUnit.YEARS);
            }
        } catch (SQLException e) {
            System.out.println(e.toString());
        }
        user_info.repaint();
        user_info.setLayout(null);
//        用户头像
        if(sex=='男')
            touxiang = new ImageIcon("images/男性图像.png");
        else
            touxiang = new ImageIcon("images/女性图像.png");
        JLabel user_img = new JLabel(touxiang);
        user_img.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        user_img.setSize(touxiang.getIconWidth(), touxiang.getIconHeight());
        user_img.setBounds(70,20, touxiang.getIconWidth(), touxiang.getIconHeight());
        user_info.add(user_img);
//        用户基本信息
//        姓名
        JLabel name_info = new JLabel("姓名："+name,SwingConstants.LEFT);
        name_info.setFont(font);
        name_info.setBounds(30,200,240,20);
        user_info.add(name_info);
//        性别
        JLabel sex_info = new JLabel("性别："+sex,SwingConstants.LEFT);
        sex_info.setFont(font);
        sex_info.setBounds(30,220,240,20);
        user_info.add(sex_info);
//        年龄
        JLabel age_info = new JLabel("年龄："+age+"岁",SwingConstants.LEFT);
        age_info.setFont(font);
        age_info.setBounds(30,240,240,20);
        user_info.add(age_info);
//        体质类型得分
        JLabel score_t = new JLabel("最近一次测试结果：",SwingConstants.CENTER);
        score_t.setFont(new Font("宋体",Font.BOLD,18));
        score_t.setBounds(30,270,240,20);
        user_info.add(score_t);
//        实际分值
        JLabel[] score_arr = new JLabel[9];
        int le = 270;
        for (int i = 0; i < score.length; i++) {
            score_arr[i] = new JLabel(type[i]+"："+score[i],SwingConstants.LEFT);
            score_arr[i].setFont(font);
            le += 30;
            score_arr[i].setBounds(30,le,240,20);
            user_info.add(score_arr[i]);
        }
//        总评结果
        JLabel summary;
        if(al_sum==null)
            summary = new JLabel("暂时无法做出准确判断",SwingConstants.CENTER);
        else
            summary = new JLabel("分析结果：",SwingConstants.LEFT);
        summary.setFont(new Font("楷体",Font.BOLD,18));
        le += 30;
        summary.setBounds(30,le,240,20);
        user_info.add(summary);
        if(al_sum!=null){
            why = new JButton("点击查看");
            why.setBounds(150,le,130,20);
            why.setFont(new Font("楷体",Font.PLAIN,18));
            user_info.add(why);
            why.addActionListener(this);
        }
        out.setBounds(50,le+50,200,35);
        out.setFont(new Font("黑体",Font.BOLD,25));
        user_info.add(out);
        out.addMouseListener(this);
        user_info.add(setBkg("images/用户背景1.png"));
        user_info.updateUI();
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==why){
            new ResultUI("结果概况",al_sum);
        }
    }
}
